package com.yanqu.road.dao.impl.tradewar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.tradewar.CrossTradeWarNotice;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarNoticeDaoImpl extends TempDataObjectDao<CrossTradeWarNotice> {

	public LinkedList<CrossTradeWarNotice> getCrossTradeWarNoticeMapByActivityId(int activityId,long selectTime){
		ResultSet rs = null;
		CallableStatement call = null;
		LinkedList<CrossTradeWarNotice> resultList= new  LinkedList<>();
		String sql = "select * from t_c_trade_war_notice where activityId = ? and noticeTime >=? order by noticeTime desc";
		try {
			getDb().setConnName(DbNameStrategy.getCrossDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1,activityId);
			call.setLong(2, selectTime);
			rs = call.executeQuery();
			while (rs.next()) {
				CrossTradeWarNotice data = parse(rs);
				resultList.addFirst(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return resultList;
	}

	public Map<Integer, LinkedList<CrossTradeWarNotice>> getCrossTradeWarNoticeMap(List<Integer> activityIdList, long selectTime) {
		ResultSet rs = null;
		CallableStatement call = null;
		Map<Integer, LinkedList<CrossTradeWarNotice>> dataMap = new ConcurrentHashMap<>();
		if(null == activityIdList || activityIdList.size() <= 0){
			return dataMap;
		}
		String sql = "select * from t_c_trade_war_notice where activityId in("+ StringUtils.listToString(activityIdList, ",") +
			") and noticeTime >=? order by noticeTime desc";
		try {
			getDb().setConnName(DbNameStrategy.getCrossDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, selectTime);
			rs = call.executeQuery();
			while (rs.next()) {
				CrossTradeWarNotice data = parse(rs);
				if(!dataMap.containsKey(data.getActivityId())){
					dataMap.put(data.getActivityId(), new LinkedList<>());
				}
				LinkedList<CrossTradeWarNotice> dataList = dataMap.get(data.getActivityId());
				dataList.addFirst(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(CrossTradeWarNotice data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_c_trade_war_notice(activityId,userId,userBaseInfo,killPatronsCount,noticeTime,enemyUserId,enemyBaseInfo," +
				"battleType,isAllKill) VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getCrossDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getUserBaseInfoJsonStr());
			call.setInt(i++, data.getKillPatronsCount());
			call.setLong(i++, data.getNoticeTime());
			call.setLong(i++, data.getEnemyUserId());
			call.setString(i++, data.getEnemyBaseInfoJsonStr());
			call.setInt(i++, data.getBattleType());
			call.setBoolean(i++, data.isAllKill());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(CrossTradeWarNotice data) {
		return false;
	}

	@Override
	protected CrossTradeWarNotice parseData(ResultSet rs) throws SQLException {
		CrossTradeWarNotice data = new CrossTradeWarNotice();
		data.setActivityId(rs.getInt("activityId"));
		data.setUserId(rs.getLong("userId"));
		String baseUserInfoStr = rs.getString("userBaseInfo");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setUserBaseInfo(userBaseInfo);
		}
		data.setKillPatronsCount(rs.getInt("killPatronsCount"));
		data.setNoticeTime(rs.getLong("noticeTime"));
		data.setEnemyUserId(rs.getLong("enemyUserId"));
		baseUserInfoStr = rs.getString("enemyBaseInfo");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setEnemyBaseInfo(userBaseInfo);
		}
		data.setBattleType(rs.getInt("battleType"));
		data.setAllKill(rs.getBoolean("isAllKill"));

		return data;
	}
}
